package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {


    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private ProvinceInfoService provinceInfoService;
    @Autowired
    private CityInfoService cityInfoService;
    @Autowired
    private DistrictInfoService districtInfoService;
    @Autowired
    private RoomInfoService roomInfoService;

    @Override
    public void customSaveOrUpdate(ApartmentSubmitVo apartmentSubmitVo) {
        //查询省市区
        Long provinceId = apartmentSubmitVo.getProvinceId();
        ProvinceInfo provinceInfoId = provinceInfoService.getById(provinceId);
        apartmentSubmitVo.setProvinceName(provinceInfoId.getName());
        Long cityId = apartmentSubmitVo.getCityId();
        CityInfo cityInfoId = cityInfoService.getById(cityId);
        apartmentSubmitVo.setCityName(cityInfoId.getName());
        Long districtId = apartmentSubmitVo.getDistrictId();
        DistrictInfo districtInfoServiceById = districtInfoService.getById(districtId);
        apartmentSubmitVo.setDistrictName(districtInfoServiceById.getName());
        //判断是更新还是保存
        boolean isUpdate = apartmentSubmitVo.getId() != null;
        //不管更新还是保存,公寓表都是单表操作,可以直接调用分类的方法
        saveOrUpdate(apartmentSubmitVo);
        //获取公寓id
        Long apartmentId = apartmentSubmitVo.getId();
        //如果是更新就需要删除中间表的数据(公寓标签,公寓配套,公寓杂费值,公寓图片)
        if (isUpdate) {
            //删除公寓标签中间表
            LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper
                    = new LambdaQueryWrapper<>();
            apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, apartmentId);
            apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);
            //删除公寓配套中间表
            LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper
                    = new LambdaQueryWrapper<>();
            apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, apartmentId);
            apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);
            //删除公寓杂费值
            LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper
                    = new LambdaQueryWrapper<>();
            apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, apartmentId);
            apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);
            //删除图片表
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, apartmentId);
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);
        }
        //保存数据
        //保存公寓标签中间表
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIds)) {
            ArrayList<ApartmentLabel> apartmentLabels = new ArrayList<>(labelIds.size());
            for (Long labelId : labelIds) {
                ApartmentLabel build = ApartmentLabel.builder().apartmentId(apartmentId).labelId(labelId).build();
                apartmentLabels.add(build);
            }
            apartmentLabelService.saveBatch(apartmentLabels);
        }
        //保存公寓配套中间表
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            ArrayList<ApartmentFacility> apartmentFacilities = new ArrayList<>(facilityInfoIds.size());
            for (Long facilityInfoId : facilityInfoIds) {
                ApartmentFacility build = ApartmentFacility.builder().apartmentId(apartmentId).facilityId(facilityInfoId).build();
                apartmentFacilities.add(build);
            }
            apartmentFacilityService.saveBatch(apartmentFacilities);
        }
        //保存公寓杂费中间表
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)) {
            ArrayList<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue build = ApartmentFeeValue.builder().apartmentId(apartmentId).feeValueId(feeValueId).build();
                apartmentFeeValues.add(build);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValues);
        }
        //保存图片表
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            ArrayList<GraphInfo> graphInfos = new ArrayList<>(graphVoList.size());
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setName(graphVo.getName());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(apartmentId);
                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }
    }

    @Override
    public void customPage(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        apartmentInfoMapper.customPage(page, queryVo);
    }

    @Override
    public ApartmentDetailVo customFind(Long id) {
        //先查询公寓的主表
        ApartmentInfo apartmentIfo = getById(id);
        //查询公寓中间表信息(公寓标签,公寓图片,公寓配套,公寓杂费)
        //查询公寓标签
        List<LabelInfo> labelInfoList = labelInfoMapper.customgetById(id);
        //查询公寓图片
        List<GraphVo> graphInfoList = graphInfoMapper.customgetById(ItemType.APARTMENT, id);
        //查询公寓配套
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.customgetById(id);
        //查询公寓杂费
         List<FeeValueVo> feeValueList =  feeValueMapper.customgetById(id);
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        apartmentDetailVo.setGraphVoList(graphInfoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        BeanUtils.copyProperties(apartmentIfo,apartmentDetailVo);
        return apartmentDetailVo;
    }

    @Override
    public void customRemovById(Long id) {
        //查询公寓有没有房间,如果有就要先处理房间,在进行删除
        LambdaQueryWrapper<RoomInfo> roomInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<RoomInfo> eq = roomInfoLambdaQueryWrapper.eq(RoomInfo::getApartmentId, id);
        long count = roomInfoService.count(eq);
        if (count>0){
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
        //删除公寓主表
        removeById(id);
        //删除中间表关系(公寓标签,公寓图片,公寓配套,公寓杂费)
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper
                = new LambdaQueryWrapper<>();
        apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
        apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.APARTMENT);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);
        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
        apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);
        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,id);
        apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);
    }
}




